var account = {};

account.egrafi = function(forma) {
	if (!account.checks(forma, true)) {
		return false;
	}

	var req = newRequest();
	if (notSet(req)) {
		return false;
	}

	req.onreadystatechange = function() {
		account.egrafiCheck(req);
	};
	openRequest(req, 'account/egrafi');

	var params = 'login=' + forma.login.value.uri();
	params += '&onoma=' + forma.onoma.value.uri();
	params += '&email=' + forma.email.value.uri();
	params += '&kodikos=' + forma.kodikos1.value.uri();

	req.send(params);
	return false;
}

account.egrafiCheck = function(req) {
	if (req.readyState != 4) {
		return;
	}
	if (req.status != 200) {
		fyi('Η εγγραφή του παίκτη απέτυχε!');
		return;
	}	
	if (!req.responseText.match(/@ok@$/)) {
		fyi(req.responseText);
		return;
	}

	window.location = globals.server + 'index.php';
}

account.enimerosi = function(forma) {
	if (!account.checks(forma, false)) {
		return false;
	}

	var req = newRequest();
	if (notSet(req)) {
		return false;
	}

	req.onreadystatechange = function() {
		account.enimerosiCheck(req);
	};
	openRequest(req, 'account/enimerosi');

	var params = 'login=' + forma.login.value.uri();
	params += '&onoma=' + forma.onoma.value.uri();
	params += '&email=' + forma.email.value.uri();
	params += '&kodikos=' + forma.kodikos1.value.uri();
	params += '&trexon=' + forma.kodikos.value.uri();

	req.send(params);
	return false;
}

account.enimerosiCheck = function(req) {
	if (req.readyState != 4) {
		return;
	}
	if (req.status != 200) {
		fyi('Η ενημέρωση απέτυχε!');
		return;
	}	
	if (!req.responseText.match(/@ok@$/)) {
		fyi(req.responseText);
		return;
	}

	fyi('Η ενημέρωση ολοκληρώθηκε με επιτυχία');
}

account.checks = function(forma, egrafi) {
	if (egrafi) {
		forma.login.value = forma.login.value.trim();	
	}
	forma.onoma.value = forma.onoma.value.trim();	
	forma.email.value = forma.email.value.trim();	

	var errfld = null;

	if (egrafi) {
		if (forma.kodikos1.value === '') {
			globals.misc.setInputColor(forma.kodikos1, globals.colors.error);
			errfld = forma.kodikos1;
		}
	}

	if (forma.kodikos1.value !== forma.kodikos2.value) {
		globals.misc.setInputColor(forma.kodikos2, globals.colors.error);
		errfld = forma.kodikos2;
	}

	if ((forma.email.value != '') && (!forma.email.value.validEmail())) {
		globals.misc.setInputColor(forma.email, globals.colors.error);
		errfld = forma.email;
	}

	if (egrafi) {
		if (!forma.login.value.validLogin()) {
			globals.misc.setInputColor(forma.login, globals.colors.error);
			errfld = forma.login;
		}
	}

	if (isSet(errfld)) {
		errfld.focus();
		return false;
	}

	return true;
}
